"use strict"
/// <reference path="../../index.d.ts" />
const Controller = require("egg").Controller
const {sendStat} = require("../extend/helper")
class AdsController extends Controller {
	constructor(ctx) {
		super(ctx)
		this.constant = this.config.constant
		this.serverConfig = this.config.serverConfig
		this.Ads = this.app.model.Ads
	}

	/* 	async getAdList() {/// 取消的接口
		this.ctx.validate({app_id: "string"}, this.ctx.query)
		let ads = await this.app.cache.ads
		let myAds = ads.filter((val) => {
			return val.stat == 1 && val.aid == this.ctx.wechat.aid
		}).map((v) => {
			let {stat, image_url, ...val} = v
			val.image_url = image_url ? this.serverConfig.imgServer + image_url : null
			return val
		})
		this.ctx.body = sendStat(this.constant.NORMAL, myAds)
	} */

	// 管理员当前应用
	async getAllAdList() {
		this.app.cache.ads = undefined// 清除缓存

		let ads = await this.app.cache.ads
		let myAds = ads.filter((val) => {
			return val.aid == this.ctx.wechat.aid
		})
		this.ctx.body = sendStat(this.constant.NORMAL, myAds)
	}
	// 应用.
	async getAppAdList() {
		this.ctx.validate({aid: "pintstr"}, this.ctx.query)
		let ads = await this.app.cache.ads
		let myAds = ads.filter((val) => {
			return val.stat == 1 && val.aid == this.ctx.query.aid
		}).map((v) => {
			let {image_url, ...val} = v
			delete val.stat
			val.image_url = image_url ? this.serverConfig.imgServer + image_url : null
			return val
		})// 广告
		this.ctx.body = sendStat(this.constant.NORMAL, myAds)
	}

	async upSertAds() {
		this.app.cache.ads = undefined// 清除缓存
		let body = this.ctx.request.body
		let initAds = {
			type: undefined,
			ad_id: undefined,
			show_type: null,
			key: null,
			app_id: null,
			title: null,
			link: null,
			remarks: undefined,
			image_url: null,
			position: undefined,
			stat: undefined,
			share_img: null
		}
		let fields = ["type", "ad_id", "show_type", "key", "app_id", "title", "link", "remarks", "image_url", "position", "stat", "share_img"]
		this.ctx.validate({
			type: ["1", "2", "3"],
			edit: ["0", "1"]
		}, body)
		let edit = Number(body.edit)
		if (edit) {
			this.ctx.validate({
				id: "pintstr"
			}, body)
		}
		let createStat
		let queryData = {id: body.id}
		if (body.type === "1") {
			this.ctx.validate({
				ad_id: "string",
				key: "string",
				stat: "numstr",
				remarks: "string",
				show_type: ["1", "2"]
			}, body)
			createStat = await this.Ads.upsert(Object.assign(initAds, {
				type: body.type,
				ad_id: body.ad_id,
				key: body.key,
				stat: body.stat,
				remarks: body.remarks,
				aid: this.ctx.wechat.aid,
				show_type: body.show_type,
				...queryData
			}), {
				// fields: ["type", "ad_id", "key", "remarks", "stat", "show_type"]
				fields
				// 注意检测aid 来源
			})
		} else if (body.type === "2") {
			// 跳转广告
			this.ctx.validate({
				app_id: "alphanum",
				image_url: "string",
				key: "string",
				link: {
					type: "string",
					required: false,
					allowEmpty: true
				},
				stat: "numstr",
				title: "string",
				remarks: "string",
			}, body)
			createStat = await this.Ads.upsert(Object.assign(initAds, {
				type: body.type,
				app_id: body.app_id,
				key: body.key, image_url: body.image_url,
				link: body.link,
				stat: body.stat, title: body.title, remarks: body.remarks,

				aid: this.ctx.wechat.aid,
				...queryData
			}), {
				fields
				// : ["type", "app_id", "key", "link", "stat", "title", "remarks", "image_url"]
			})
		} else if (body.type === "3") {
			this.ctx.validate({
				image_url: "string",
				share_img: "string",
				key: "string",
				stat: "numstr",
				title: "string",
				remarks: "string",
			}, body)
			createStat = await this.Ads.upsert(Object.assign(initAds, {
				type: body.type,
				app_id: body.app_id,
				key: body.key, image_url: body.image_url,
				link: body.link,
				stat: body.stat, title: body.title, remarks: body.remarks,
				share_img: body.share_img,
				aid: this.ctx.wechat.aid,
				...queryData
			}), {
				fields
				// : ["type", "key", "stat", "title", "remarks", "share_img", "image_url"]
			})
		}
		if (createStat || edit == 1) {
			this.ctx.body = sendStat(this.constant.NORMAL)
		} else {
			this.ctx.body = sendStat(this.constant.ERROR_SQL_CREATE)
		}
	}


}
module.exports = AdsController